﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XrmLibrary.MsCrm4Adapter.Extensions
{
    public static class PrincipalAccessExtensions
    {
        public static Microsoft.Crm.Sdk.Messages.PrincipalAccess ToCrm2011PrincipalAccess(this Microsoft.Crm.Sdk.PrincipalAccess crm4PrincipalAccess)
        {
            Microsoft.Crm.Sdk.Messages.PrincipalAccess crm2011PrincipalAccess =
                new Microsoft.Crm.Sdk.Messages.PrincipalAccess
                {
                    AccessMask = crm4PrincipalAccess.AccessMask.ToCrm2011AccessRights(),
                    Principal = crm4PrincipalAccess.Principal.ToEntityReference()
                };

            return crm2011PrincipalAccess;
        }

        public static IEnumerable<Microsoft.Crm.Sdk.Messages.PrincipalAccess> ToCrm2011PrincipalAccessCollection(this Microsoft.Crm.Sdk.PrincipalAccess[] crm4PrincipalAccess)
        {
            foreach (Microsoft.Crm.Sdk.PrincipalAccess principalAccess in crm4PrincipalAccess)
            {
                yield return principalAccess.ToCrm2011PrincipalAccess();
            }
        }

        public static Microsoft.Crm.Sdk.PrincipalAccess ToCrm4PrincipalAccess(this Microsoft.Crm.Sdk.Messages.PrincipalAccess crm2011PrincipalAccess)
        {
            Microsoft.Crm.Sdk.PrincipalAccess crm4PrincipalAccess =
                new Microsoft.Crm.Sdk.PrincipalAccess
                {
                    AccessMask = crm2011PrincipalAccess.AccessMask.ToCrm4AccessRights(),
                    Principal = crm2011PrincipalAccess.Principal.ToCrm4SecurityPrincipal()
                };

            return crm4PrincipalAccess;
        }
    }
}
